package kr.hkit.androidcomplete01.ch17;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;

public class SaveState01 extends Activity {
	private StateView vw;
	int x;
	int y;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if(savedInstanceState == null){
			x = 50;
			
		}else{//임시저장
			x = savedInstanceState.getInt("x");
		}
		SharedPreferences pref = getSharedPreferences("", 0);
		y = pref.getInt("y", 50);//영구저장
		
		vw = new StateView(this);
		vw.setPt_x(x);
		vw.setPt_y(y);
		
		vw.setFocusable(true);
		vw.setFocusableInTouchMode(true);
		setContentView(vw);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		SharedPreferences pref = getSharedPreferences("SaveState", 0);
		SharedPreferences.Editor edit = pref.edit();
		edit.putInt("y", vw.getPt_y());
		edit.commit();
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt("x", vw.getPt_x());
	}
	
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		vw.setPt_x(savedInstanceState.getInt("x"));
		vw.invalidate();
	}
	
}